package com.jlastu.dlxy_androidapp.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jlastu.dlxy_androidapp.database.DBHelper;

import java.util.Objects;


public class ZNBProvider extends ContentProvider {

    private static final String AUTHORITY = "com.jlastu.dlxy_androidapp.provider";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int MATCHER_GOODS = 1;
    private static final int MATCHER_GOODS_ID = 2;
    private static final int MATCHER_GOODS_NAME = 3;
    private static final int MATCHER_GOODS_CLASSIFY = 4;
    private static final int MATCHER_STORE_GOODS = 5;
    private static final int MATCHER_USER = 11;
    private static final int MATCHER_USER_ID = 12;

    private static final int MATCHER_USER_NAME = 13;


    private static final int MATCHER_PHOTO = 21;

    private static final int MATCHER_SCHOOL = 22;

    private static final int MATCHER_SCHOOL_PHOTO = 23;
    private static final int MATCHER_SCHOOL_CLASSIFY = 24;
    private static final int MATCHER_SCHOOL_ID = 25;
    private static final int MATCHER_STORE = 26;
    private static final int MATCHER_STORE_ID = 27;


    private DBHelper mDBHelper;

    static {//通配符：*所有字符，#所有数字
        // content://com.jlastu.dlxy_androidapp.provider/goods
        uriMatcher.addURI(AUTHORITY, "goods", MATCHER_GOODS);
        // content://com.jlastu.dlxy_androidapp.provider/goods/1
        uriMatcher.addURI(AUTHORITY, "goods/#", MATCHER_GOODS_ID);
        uriMatcher.addURI(AUTHORITY, "store_goods/#", MATCHER_STORE_GOODS);
        uriMatcher.addURI(AUTHORITY, "goodsname/*", MATCHER_GOODS_NAME);
        uriMatcher.addURI(AUTHORITY, "goodsclassify/*", MATCHER_GOODS_CLASSIFY);
        uriMatcher.addURI(AUTHORITY, "user", MATCHER_USER);
        uriMatcher.addURI(AUTHORITY, "user/#", MATCHER_USER_ID);
        uriMatcher.addURI(AUTHORITY, "username/*", MATCHER_USER_NAME);
        uriMatcher.addURI(AUTHORITY, "photo", MATCHER_PHOTO);
        uriMatcher.addURI(AUTHORITY, "school", MATCHER_SCHOOL);
        uriMatcher.addURI(AUTHORITY, "school/#", MATCHER_SCHOOL_ID);
        uriMatcher.addURI(AUTHORITY, "schoolphoto", MATCHER_SCHOOL_PHOTO);
        uriMatcher.addURI(AUTHORITY, "schoolclassify/*", MATCHER_SCHOOL_CLASSIFY);
        uriMatcher.addURI(AUTHORITY, "store/#", MATCHER_STORE_ID);
        uriMatcher.addURI(AUTHORITY, "store", MATCHER_STORE);

    }

    @Override
    public boolean onCreate() {
        mDBHelper = new DBHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri,
                        @Nullable String[] projection,
                        @Nullable String selection,
                        @Nullable String[] selectionArgs,
                        @Nullable String sortOrder) {
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                return db.query("goods", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                return db.query("goods", projection, "_id = ?",
                        new String[]{String.valueOf(goods_id)}, null,
                        null, sortOrder, null);
            case MATCHER_USER:
                return db.query("user", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);
                Log.d("provider", "user_id ========= " + user_id);
                return db.query("user", projection, "_id = ?",
                        new String[]{String.valueOf(user_id)}, null,
                        null, sortOrder, null);
            case MATCHER_USER_NAME:
                String user_name = uri.getPathSegments().get(1);
                return db.query("user", projection, "user_name = ?",
                        new String[]{String.valueOf(user_name)}, null,
                        null, sortOrder, null);
            case MATCHER_PHOTO:
                Cursor cursor = db.query("photo", projection, selection,
                        selectionArgs, null, null, sortOrder);
                CursorWindow cw = new CursorWindow("test", 5000000);
                AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
                ac.setWindow(cw);
                return cursor;
            case MATCHER_GOODS_NAME:
                String goods_name = uri.getPathSegments().get(1);
                return db.query("goods", projection, "goods_name = ?",
                        new String[]{String.valueOf(goods_name)}, null,
                        null, sortOrder, null);
            case MATCHER_GOODS_CLASSIFY:
                String goods_classify = uri.getPathSegments().get(1);
                return db.query("goods", projection, "goods_classify = ?",
                        new String[]{String.valueOf(goods_classify)}, null,
                        null, sortOrder, null);
            case MATCHER_SCHOOL:
                return db.query("school", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_SCHOOL_PHOTO:
                Cursor schoolcursor = db.query("schoolphoto", projection, selection,
                        selectionArgs, null, null, sortOrder);
                CursorWindow scw = new CursorWindow("test", 5000000);
                AbstractWindowedCursor sac = (AbstractWindowedCursor) schoolcursor;
                sac.setWindow(scw);
                return schoolcursor;
            case MATCHER_SCHOOL_CLASSIFY:
                String school_classify = uri.getPathSegments().get(1);
                return db.query("school", projection, "school_classify = ?",
                        new String[]{String.valueOf(school_classify)}, null,
                        null, sortOrder, null);
            case MATCHER_SCHOOL_ID:
                long school_id = ContentUris.parseId(uri);
                Log.d("provider", "user_id ========= " + school_id);
                return db.query("school", projection, "_id = ?",
                        new String[]{String.valueOf(school_id)}, null,
                        null, sortOrder, null);
            case MATCHER_STORE_ID:
                long userid = Long.parseLong(uri.getPathSegments().get(1));
                return db.query("store", projection, "user_id = ?",
                        new String[]{String.valueOf(userid)}, null,
                        null, sortOrder, null);
            case MATCHER_STORE_GOODS:
                long store_id = Long.parseLong(uri.getPathSegments().get(1));
                return db.query("goods", projection, "store_id = ?",
                        new String[]{String.valueOf(store_id)}, null,
                        null, sortOrder, null);
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        long insertId = -1;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                insertId = db.insert("goods", null, values);
                break;
            case MATCHER_USER:
                insertId = db.insert("user", null, values);
                break;
            case MATCHER_PHOTO:
                insertId = db.insert("photo", null, values);
                break;
            case MATCHER_SCHOOL:
                insertId = db.insert("school", null, values);
                break;
            case MATCHER_SCHOOL_PHOTO:
                insertId = db.insert("schoolphoto", null, values);
                break;
            case MATCHER_STORE:
                insertId = db.insert("store", null, values);
                break;


        }
        return ContentUris.withAppendedId(uri, insertId);
    }


        @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        if (values == null || values.length == 0) {
            return 0;
        }
        // 获取表名，根据你的UriMatcher来确定
        String tableName = getTableNameFromUri(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        db.beginTransaction(); // 开始事务
        int numValues = values.length;
        int rowsNum = 0;

        try {
            for (ContentValues value : values) {
                long id = db.insert(tableName, null, value);
                if (id != -1) {
                    rowsNum++; // 计数器
                }
            }
            db.setTransactionSuccessful(); // 标记事务成功
        } finally {
            db.endTransaction(); // 结束事务
        }

        if (rowsNum > 0) {
            // 通知URI变化
            getContext().getContentResolver().notifyChange(uri, null);
        }

        return rowsNum; // 返回成功插入的行数
    }
//    @Override
//    public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
//        for (ContentValues contentValues : values) {
//            Log.d("", "bulkInsert ===== ");
//            insert(uri, contentValues);
//        }
//        return super.bulkInsert(uri, values);
//    }

    private String getTableNameFromUri(Uri uri) {
        // 这里是一个示例，你需要根据实际情况来实现这个方法
        switch (uriMatcher.match(uri)) {
            case MATCHER_SCHOOL_PHOTO:
                return "schoolphoto";
            case MATCHER_PHOTO:
                return "photo";
            default:
                return null;
        }
    }
    // 初始化UriMatcher的代码
    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:

            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                return db.delete("goods" ,"_id = ?",new String[]{String.valueOf(goods_id)});
            case MATCHER_USER:

            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);

        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        int rowsUpdated = 0;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:

            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);

            case MATCHER_USER:

            case MATCHER_USER_ID:
                String user_id = uri.getPathSegments().get(1);
                rowsUpdated = db.update("user", values, "_id = ?", new String[]{String.valueOf(user_id)});
                break;
            case MATCHER_STORE_ID:
                int userid = Integer.parseInt(uri.getPathSegments().get(1));
                rowsUpdated = db.update("store", values, "user_id = ?", new String[]{String.valueOf(userid)});
                break;
        }
        return rowsUpdated;
    }
}
